package com.bootdo.contract.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.contract.domain.TestpaperDO;
import com.bootdo.contract.service.TestpaperService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * @author yang
 * @email 2795210596@qq.com
 * @date 2019-09-09 15:53:49
 */

@Controller
@RequestMapping("/contract/testpaper")
public class TestpaperController {
    @Autowired
    private TestpaperService testpaperService;

    @GetMapping()
    String Testpaper() {
        return "contract/testpaper/testpaper";
    }

    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<TestpaperDO> testpaperList = testpaperService.list(query);
        int total = testpaperService.count(query);
        PageUtils pageUtils = new PageUtils(testpaperList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    String add() {
        return "contract/testpaper/add";
    }

    @GetMapping("/edit/{id}")
    String edit(@PathVariable("id") Integer id, Model model) {
        TestpaperDO testpaper = testpaperService.get(id);
        model.addAttribute("testpaper", testpaper);
        return "contract/testpaper/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    public R save(TestpaperDO testpaper) {
        Date date = new Date();
        testpaper.setCreatetime(new Date());
        Date begintime = testpaper.getBegintime();
        Date endtime = testpaper.getEndtime();
        if (((endtime.getTime() - begintime.getTime()) / 1000 / 60) < 30) {
            return R.error("考试时间不得少于30分钟");
        }
        if (date.getTime() > begintime.getTime()) {
            return R.error("开始时间不得小于现在时间");
        }
        if (date.getTime() > endtime.getTime()) {
            return R.error("结束时间不得小于现在时间");
        }
        if (testpaperService.save(testpaper) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    public R update(TestpaperDO testpaper) {
        testpaperService.update(testpaper);
        return R.ok();
    }


    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    public R remove(Integer id) {
        if (testpaperService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    public R remove(@RequestParam("ids[]") Integer[] ids) {
        testpaperService.batchRemove(ids);
        return R.ok();
    }

}
